let win: any = window;

const DBCursor = win.IDBCursor;
const DBKeyRange = win.IDBKeyRange || win.webkitIDBKeyRange || win.msIDBKeyRange;
export const innerDB = win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB;
const DBTransaction = win.IDBTransaction || win.webkitIDBTransaction || win.msIDBTransaction;
/**
 * 事务的模式
 * READ：读
 * READ_WRITE：读写
 * VERION_CHANGE: 版本变更
 * @type {{READ: string, READ_WRITE: string, VERION_CHANGE: string}}
 */
export enum TransactionMode {
    READ_ONLY='readonly',
    READ_WRITE='readwrite',
    READ_WRITE_FLUSH='readwriteflush',
    VERION_CHANGE='verionchange'
}

/*
 * 游标循环的方向
 * CursorDirection.NEXT                     顺序循环
 * CursorDirection.NEXT_NO_DUPLICATE        顺序循环不重复.当取值有重复时，只获取一次。
 * CursorDirection.PREV                     倒序循环
 * CursorDirection.PREV_NO_DUPLICATE        倒序循环不重复.当取值有重复时，只获取一次。
 * @type {{NEXT: *, NEXT_NO_DUPLICATE: *, PREV: *, PREV_NO_DUPLICATE: *}}
 */
export enum CursorDirection{
    NEXT = DBCursor.NEXT,
    NEXT_NO_DUPLICATE = DBCursor.NEXT_NO_DUPLICATE,
    PREV = DBCursor.PREV,
    PREV_NO_DUPLICATE = DBCursor.PREV_NO_DUPLICATE
}

export class KeyRange {
    static leq (x:any) {
        return DBKeyRange.upperBound(x) //All keys ≤ x
    }
    static lt (x:any) {
        return DBKeyRange.upperBound(x, true) //keys < x
    }
    static geq (x:any) {
        return DBKeyRange.lowerBound(x)//keys ≥ x
    }
    static gt (x:any) {
        return DBKeyRange.lowerBound(x, true) //keys > x
    }
    static between (x:any, y:any) {
        return DBKeyRange.bound(x, y)//keys ≥ x && ≤ y
    }
    static gt_lt (x:any, y:any) {
        return DBKeyRange.bound(x, y, true, true)//keys > x &&< y
    }
    static gt_leq (x:any, y:any) {
        return DBKeyRange.bound(x, y, true, false)//keys > x && ≤ y
    }
    static geq_lt (x:any, y:any) {
        return DBKeyRange.bound(x, y, false, true)//keys ≥ x &&< y
    }
    static eq (x:any, y:any) {
        return DBKeyRange.only(x)//key = x
    }
}

export enum IndexdbStatus {
    CONNECT_ING=0,
    CONNECT_SUCCESS=1,
    CONNECT_ERROR=2,
    CONNECT_CHANGE=3,
    CONNECT_CLOSE=4,
    DATABASE_ERROR=0
}
